//
//  MDPhoneController.swift
//  麦兜直播
//
//  Created by Apple on 16/10/21.
//  Copyright © 2016年 yeqingyu. All rights reserved.
//

import UIKit
import AVFoundation

private let menuHeight:CGFloat = 49.0
private let btnWidth:CGFloat = 100
private let menuMarginX:CGFloat = 20
class MDPhoneController: UIViewController {
    // MARK:- 视频输入源的全局队列
    fileprivate lazy var videoOutPutQueue = DispatchQueue.global()
    // MARK:-1.创建捕捉会话
    fileprivate lazy var session:AVCaptureSession = AVCaptureSession()
    // MARK:- 创建预览图层
    fileprivate lazy var previewLayer:AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.session)
    
    fileprivate lazy var menuView:UIView = {
        let rect = CGRect(x: 0, y: 0, width: screenWidth, height: menuHeight)
        let menuView = UIView(frame: rect)
        menuView.backgroundColor = UIColor(r: 0, g: 0, b: 0, a: 0.3)
        //1.床架开始录制按钮
        let starCapBtnRect = CGRect(x: menuMarginX, y: 0, width: btnWidth, height: menuHeight)
        let starCapBtn = UIButton(frame: starCapBtnRect)
        starCapBtn.setTitle("开始录制", for: .normal)
        starCapBtn.addTarget(self, action: #selector(MDPhoneController.starCapture), for: UIControlEvents.touchUpInside)
        //2.创建结束录制按钮
        let stopCapBtn = UIButton()
        stopCapBtn.frame.size = CGSize(width: btnWidth, height: menuHeight)
        stopCapBtn.center = menuView.center
        stopCapBtn.setTitle("结束录制", for: .normal)
        stopCapBtn.addTarget(self, action: #selector(MDPhoneController.stopCapture), for: UIControlEvents.touchUpInside)
        
        //3.切换镜头按钮
        let changeCapBtnRect = CGRect(x: screenWidth-menuMarginX-btnWidth, y: 0, width: btnWidth, height: menuHeight)
        let changeCapBtn = UIButton(frame: changeCapBtnRect)
        changeCapBtn.setTitle("切换镜头", for: .normal)
        changeCapBtn.addTarget(self, action: #selector(MDPhoneController.changeCapture), for: UIControlEvents.touchUpInside)
        
        menuView.addSubview(starCapBtn)
        menuView.addSubview(stopCapBtn)
        menuView.addSubview(changeCapBtn)
        return menuView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.purple
        setUpTopMenuView()

    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        dismiss(animated: true) { 
            
        }
    }
}
extension MDPhoneController{
    func setUpTopMenuView(){
        //添加头部菜单视图
        view.addSubview(menuView)
    }
    
    // MARK:- 开始录制视频
    func starCapture(){
        print("开始录制")
        //2.给会话设置输入源(摄像头)
        guard  let divices =  AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) as? [AVCaptureDevice] else{
            print("获取设备失败")
            return
        }
        print(divices)
        guard let divice = divices.filter({$0.position == .front}).first else {return}
        guard let videoInput = try?AVCaptureDeviceInput(device: divice) else{return}
        session.addInput(videoInput)
        
        //3.给会话设置输出源
        let videoOutput = AVCaptureVideoDataOutput()
        videoOutput.setSampleBufferDelegate(self, queue: videoOutPutQueue)
        session.addOutput(videoOutput)
        //4.给用户设置一个预览图层
        previewLayer.frame = view.frame
        view.layer.insertSublayer(previewLayer, at: 0)
        print(view.layer)
        //5.开始捕捉
        session .startRunning()
        
    }
    
    // MARK:- 结束录制视频
    func stopCapture(){
        print("结束录制")
        session .stopRunning()
        previewLayer.removeFromSuperlayer()
    }
    
    // MARK:- 切换镜头
    func changeCapture(){
        print("切换镜头")
        
    }
}


extension MDPhoneController :AVCaptureVideoDataOutputSampleBufferDelegate{
    func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
        print("输出数据")
    }
}
